package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.collect.Lists;
import com.zhijian.medical.dao.local.CnasFileMapper;
import com.zhijian.medical.entity.po.CnasFile;
import com.zhijian.medical.entity.po.CnasSortName;
import com.zhijian.medical.entity.vo.request.CnasFileSearchVo;
import com.zhijian.medical.entity.vo.response.CnasFileAllVo;
import com.zhijian.medical.entity.vo.response.CnasFileVo;
import com.zhijian.medical.entity.vo.response.NewFileVo;
import com.zhijian.medical.enums.CnasFileEnum;
import com.zhijian.medical.service.CnasFileService;
import com.zhijian.medical.service.CnasSortNameService;
import com.zhijian.medical.service.NewFileService;
import com.zhijian.medical.util.AssertUtil;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * cnas认可相关文件业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2023-03-10 17:47:33
 */
@Slf4j
@Service
public class CnasFileServiceImpl implements CnasFileService {

    @Autowired
    private CnasFileMapper mapper;

    @Autowired
    private NewFileService newFileService;

    @Autowired
    private CnasSortNameService cnasSortNameService;

    @Override
    public CnasFile selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public CnasFileVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(CnasFile cnasFile) {
        Date now = new Date();
        cnasFile.setCreateTime(now);
        cnasFile.setUpdateTime(now);
        return mapper.insert(cnasFile) > 0 ? cnasFile.getId() : 0L;
    }

    @Override
    public boolean update(CnasFile cnasFile) {
        Date now = new Date();
        cnasFile.setUpdateTime(now);
        return mapper.updateByPrimaryKey(cnasFile) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(CnasFile record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public Integer count(CnasFileSearchVo cnasFileSearchVo) {
        return mapper.count(cnasFileSearchVo);
    }

    @Override
    public List<CnasFileVo> selectList(CnasFileSearchVo cnasFileSearchVo) {
        return mapper.selectList(cnasFileSearchVo);
    }

    @Override
    public Page<CnasFileVo> selectPage(CnasFileSearchVo cnasFileSearchVo) {
        //先直接查总数
        int count = count(cnasFileSearchVo);
        //分页
        Page<CnasFileVo> page = new Page<>(true, count, cnasFileSearchVo.getPageNum(), cnasFileSearchVo.getPageSize());
        cnasFileSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<CnasFileVo> responseVos = this.selectList(cnasFileSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<CnasFile> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public boolean addKsSlefFile(Long sortNameId, Long fileId) {
        return this.insert(CnasFile.builder()
                .sortNameId(sortNameId)
                .fileId(fileId)
                .type(CnasFileEnum.KSZJ.getCode().byteValue())
                .build()) != 0;
    }

    @Override
    public boolean addXgzcsFile(Long sortNameId, Long fileId) {
        return this.insert(CnasFile.builder()
                .sortNameId(sortNameId)
                .fileId(fileId)
                .type(CnasFileEnum.XGZCX.getCode().byteValue())
                .build()) != 0;
    }

    @Override
    public boolean addBgFile(Long sortNameId, Long fileId, Long parentKsId) {

        List<CnasFileVo> cnasFileVos = this.selectList(CnasFileSearchVo.builder()
                .sortNameId(sortNameId)
                .fileId(parentKsId)
                .type(CnasFileEnum.KSZJ.getCode().byteValue())
                .build());
        AssertUtil.check(CollectionUtil.isEmpty(cnasFileVos), "对应的相应管理要素的文件不存在或者已被删除");

        return this.insert(CnasFile.builder()
                .sortNameId(sortNameId)
                .fileId(fileId)
                .parentId(cnasFileVos.get(0).getId())
                .type(CnasFileEnum.BG.getCode().byteValue())
                .build()) != 0;
    }

    @Override
    public CnasFileAllVo selectCnasFileAllVo(Long cnasNameId) {
        CnasFileAllVo cnasFileAllVo = new CnasFileAllVo();
        cnasFileAllVo.setSortNameId(cnasNameId);

        CnasSortName cnasSortName = cnasSortNameService.selectById(cnasNameId);

        AssertUtil.notNull(cnasSortName, "数据不存在或者已被删除");

        cnasFileAllVo.setTopBz(cnasSortName.getTopBz());

        List<CnasFileVo> ksZjCnasFileVos = this.selectList(CnasFileSearchVo.builder()
                .sortNameId(cnasNameId)
                .type(CnasFileEnum.KSZJ.getCode().byteValue())
                .build());
        if (CollectionUtil.isNotEmpty(ksZjCnasFileVos)) {
            cnasFileAllVo.setKsSlefFileList(ksZjCnasFileVos.stream().map(cnasFileVo -> {

                NewFileVo ksSlefNewFileVo = newFileService.selectVoById(cnasFileVo.getFileId());
                List<CnasFileVo> childrenCnasFileVo = this.selectList(CnasFileSearchVo.builder()
                        .sortNameId(cnasNameId)
                        .type(CnasFileEnum.BG.getCode().byteValue())
                        .parentId(cnasFileVo.getId())
                        .build());
                if (CollectionUtil.isNotEmpty(childrenCnasFileVo)) {
                    List<NewFileVo> childrenNewFileList = Lists.newArrayList();
                    childrenCnasFileVo.forEach(c -> {
                        NewFileVo k = newFileService.selectVoById(c.getFileId());
                        childrenNewFileList.add(k);
                    });
                    ksSlefNewFileVo.setChildren(childrenNewFileList);
                }
                return ksSlefNewFileVo;

            }).collect(Collectors.toList()));
        }

        List<CnasFileVo> xgxList = this.selectList(CnasFileSearchVo.builder()
                .sortNameId(cnasNameId)
                .type(CnasFileEnum.XGZCX.getCode().byteValue())
                .build());
        if (CollectionUtil.isNotEmpty(xgxList)) {
            cnasFileAllVo.setXgZcxFileList(xgxList.stream().map(c -> newFileService.selectVoById(c.getFileId())).collect(Collectors.toList()));
        }

        return cnasFileAllVo;
    }
}